package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.CacheUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CNews;
import com.ruoyi.system.service.ICNewsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 信息资讯Controller
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Controller
@RequestMapping("/system/news")
public class CNewsController extends BaseController
{
    private String prefix = "system/news";

    @Autowired
    private ICNewsService cNewsService;

    @RequiresPermissions("system:news:view")
    @GetMapping()
    public String news()
    {
        return prefix + "/news";
    }

    /**
     * 查询信息资讯列表
     */
    @RequiresPermissions("system:news:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CNews cNews)
    {
        startPage();
        List<CNews> list = cNewsService.selectCNewsList(cNews);
        return getDataTable(list);
    }

    /**
     * 导出信息资讯列表
     */
    @RequiresPermissions("system:news:export")
    @Log(title = "信息资讯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CNews cNews)
    {
        List<CNews> list = cNewsService.selectCNewsList(cNews);
        ExcelUtil<CNews> util = new ExcelUtil<CNews>(CNews.class);
        return util.exportExcel(list, "信息资讯数据");
    }

    /**
     * 新增信息资讯
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存信息资讯
     */
    @RequiresPermissions("system:news:add")
    @Log(title = "信息资讯", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CNews cNews)
    {
        return toAjax(cNewsService.insertCNews(cNews));
    }

    /**
     * 修改信息资讯
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CNews cNews = null;
        if(id == null || id ==  -1){
            cNews = new CNews();
        }else{
            cNews = cNewsService.selectCNewsById(id);
        }
        mmap.put("cNews", cNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存信息资讯
     */
    @RequiresPermissions("system:news:edit")
    @Log(title = "信息资讯", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CNews cNews)
    {

        CacheUtils.removeAll(Constants.BANNER_KEY);
        return toAjax(cNewsService.updateCNews(cNews));
    }

    /**
     * 删除信息资讯
     */
    @RequiresPermissions("system:news:remove")
    @Log(title = "信息资讯", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        CacheUtils.removeAll(Constants.BANNER_KEY);
        return toAjax(cNewsService.deleteCNewsByIds(ids));
    }
}
